package com.sortingalgorithm;

import java.util.Arrays;

public class QuickSort2 {
    public static void main(String[] args) {
        int[] data = {1,4,-4,9,23,-58,0};
        quicksort(data,0,data.length - 1);
        System.out.println(Arrays.toString(data));
    }

    private static void quicksort(int[] data,int left,int right){
        if(left >= right){
            return;
        }
        int mid = partition(data,left,right);
        quicksort(data,left,mid - 1);
        quicksort(data,mid + 1,right);

    }

    private static int partition(int[] data,int left,int right){
        int l = left,r =  right;
        int pivot = data[l];
        while(l < r){
            while (l < r && data[r] >= pivot){
                r--;
            }
            data[l] = data[r];
            while (l < r && data[l] <= pivot){
                l++;
            }
            data[r] = data[l];
        }
        data[l] = pivot;
        return l;
    }

}
